package com.ruoyi.spspublic.util;

import com.ruoyi.spspublic.domain.vo.DateStartAndEnd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/****
 * wangwei
 * 日期区间  月，季度，半年，年 开始日期 结束日期
 */
public class DateRange {

    /***月开始日期与结束日期
     * date 为时间区间值
     * ***/
    public DateStartAndEnd month(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //TODO dtBussDate改参数
        //Date dtBussDate = sdf.parse("2016-06-19 00:00:00");
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        LocalDateTime startTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        LocalDateTime endTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
        DateStartAndEnd dateStartAndEnd = new DateStartAndEnd();
        //Date newStartDate = sdf.parse(df.format(startTime));
        //Date newEndTime = sdf.parse(df.format(startTime));
        dateStartAndEnd.setStartDate(sdf.parse(df.format(startTime)));
        dateStartAndEnd.setEndDate(sdf.parse(df.format(endTime)));
        dateStartAndEnd.setNumber( startTime.getMonthValue());//月份
        sdf.format(dateStartAndEnd.getStartDate());
        sdf.format(dateStartAndEnd.getEndDate());
        return dateStartAndEnd;
    }

    /***
     * 季度开始日期与结束日期
     * @param date
     * @return
     * @throws ParseException
     */
    public DateStartAndEnd quarter(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //TODO dtBussDate改参数
       // Date dtBussDate = sdf.parse("2016-06-19 00:00:00");
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();

        Month month = Month.of(localDate.getMonth().firstMonthOfQuarter().getValue());
        //季度开始日期
        LocalDateTime startDate = LocalDateTime.of(LocalDate.of(localDate.getYear(), month, 1), LocalTime.MIN);
        LocalDate now = LocalDate.now();
        Month endMonth = Month.of(localDate.getMonth().firstMonthOfQuarter().getValue()).plus(2L);
        LocalDateTime endDate = LocalDateTime.of(LocalDate.of(localDate.getYear(), endMonth, month.length(now.isLeapYear())), LocalTime.MAX);
        DateStartAndEnd dateStartAndEnd = new DateStartAndEnd();
        dateStartAndEnd.setStartDate(sdf.parse(df.format(startDate)));
        dateStartAndEnd.setEndDate(sdf.parse(df.format(endDate)));
        int quarter = (startDate.getMonthValue()-1)/3 + 1;
        dateStartAndEnd.setNumber(quarter);//季度

        return dateStartAndEnd;
    }

    /****
     * 半年开始日期与结束日期
     * @param date
     * @return
     */
    public DateStartAndEnd halfYear(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //TODO dtBussDate改参数
        //Date dtBussDate = sdf.parse("2016-06-19 00:00:00");
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        Month month = (localDate.getMonthValue() > 6) ? Month.JULY : Month.JANUARY;
        LocalDateTime startHalfDate = LocalDateTime.of(LocalDate.of(localDate.getYear(), month, 1), LocalTime.MIN);
        Month endMonth = (localDate.getMonthValue() > 6) ? Month.DECEMBER : Month.JUNE;
        LocalDateTime endHalfdate = LocalDateTime.of(LocalDate.of(localDate.getYear(), endMonth, endMonth.length(localDate.isLeapYear())), LocalTime.MAX);
        DateStartAndEnd dateStartAndEnd = new DateStartAndEnd();
        dateStartAndEnd.setStartDate(sdf.parse(df.format(startHalfDate)));
        dateStartAndEnd.setEndDate(sdf.parse(df.format(endHalfdate)));
        return dateStartAndEnd;
    }

    /***
     * 一年开始日期与结束日期
     * @param date
     * @return
     * @throws ParseException
     */
    public DateStartAndEnd year(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //TODO dtBussDate改参数
        //Date dtBussDate = sdf.parse("2016-06-19 00:00:00");

        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();

        LocalDateTime startYearDate = LocalDateTime.of(localDate.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
        LocalDateTime endYearDate = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
        DateStartAndEnd dateStartAndEnd = new DateStartAndEnd();
        dateStartAndEnd.setStartDate(sdf.parse(df.format(startYearDate)));
        dateStartAndEnd.setEndDate(sdf.parse(df.format(endYearDate)));
        dateStartAndEnd.setNumber(startYearDate.getYear());//年份
        return dateStartAndEnd;
    }

}
